package com.controller;

import com.model.UpdateSelfInfoModel;
import com.model.operation.UserOperate;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LPFma on 2016/12/4 0004.
 */
@WebServlet(name = "UpdateSelfInformation", urlPatterns = "/UpdateSelfInformation")
public class UpdateSelfInformation extends HttpServlet {
    private final String invalid = "invalid";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*init response*/
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.addHeader("Access-Control-Allow-Origin","*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

        /* get parameters */
        String nickname = request.getParameter("nickname");
        String passwd = request.getParameter("passwd");
        String sex = request.getParameter("sex");
        String signature = request.getParameter("signature");
        String username = (String) request.getSession().getAttribute("username");

        String isNicknameValid;
        String isPasswdValid = invalid;
        String isSexValid = invalid;
        String isSignarueValid = invalid;
        String status = "fail";

        do {
            isNicknameValid = UpdateSelfInfoModel.isNicknameValid(nickname);
            if(isNicknameValid.equals(invalid)) break;

            isPasswdValid = UpdateSelfInfoModel.isPasswdValid(passwd);
            if(isPasswdValid.equals(invalid)) break;

            isSexValid = UpdateSelfInfoModel.isSexValid(sex);
            if(isSexValid.equals(invalid)) break;

            isSignarueValid = UpdateSelfInfoModel.isSignatureValid(signature);
            if(isSignarueValid.equals(invalid)) break;

            UserOperate userOperate = new UserOperate();
            status = userOperate.UpdateSelfInformation(username, passwd,
                    nickname, sex, signature);
        }while (false);

        String finalIsPasswdValid = isPasswdValid;
        String finalIsSexValid = isSexValid;
        String finalIsSignarueValid = isSignarueValid;
        String finalStatus = status;

        /* pack json map */
        Map map = new HashMap<String, String>() {{
            put("nickname", isNicknameValid);
            put("passwd", finalIsPasswdValid);
            put("sex", finalIsSexValid);
            put("signature", finalIsSignarueValid);
            put("status", finalStatus);
        }};

        /* send json */
        JSONObject jsonObject = JSONObject.fromObject(map);
        response.getWriter().write(jsonObject.toString());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
